package wxdb.metadata;

import java.io.*;

import wxdb.ui.Global;

public class DbInfoMgr {
	
	public static void writeMeta(int dbid, int tableNum){
		DbInfo dbinfo = new DbInfo(dbid, tableNum);
		String metaFileName = "metadata/" + Global.findDBName(dbid) + ".info";
		try{
			FileOutputStream fos = new FileOutputStream(metaFileName);
			ObjectOutputStream oos = new ObjectOutputStream(fos);
			oos.writeObject(dbinfo);
		}catch(IOException ioe){
			ioe.printStackTrace();
		}
	}
	
	public static void writeMeta(DbInfo din){
		String metaFileName = "metadata/" + Global.findDBName(din.getDbid()) + ".info";
		try{
			FileOutputStream fos = new FileOutputStream(metaFileName);
			ObjectOutputStream oos = new ObjectOutputStream(fos);
			oos.writeObject(din);
		}catch(IOException ioe){
			ioe.printStackTrace();
		}
	}
	
	public static DbInfo getMeta(int dbid){
		String metaFileName = "metadata/" + Global.findDBName(dbid) + ".info";
		try{
			FileInputStream fis = new FileInputStream(metaFileName);
			ObjectInputStream ois = new ObjectInputStream(fis);
			return (DbInfo)ois.readObject();
		}catch(FileNotFoundException fnfe){
			fnfe.printStackTrace();
		}catch(ClassNotFoundException cnfe){
			cnfe.printStackTrace();
		}catch(IOException ioe){
			ioe.printStackTrace();
		}
		return null;
	}
}
